(function(factory) {
	if (typeof define === "function" && define.amd) {
		// AMD. Register as an anonymous module.
		define(factory);
	} else {
		// Browser globals
		factory();
	}
}(function() {
	/**
	 * @author 李球 <service@hteos.com>
	 * @class HteOS.Settings 个性化设置应用，主题和墙纸设置
	 * @singleton
	 */
	HteOS.Settings = {
		
		defaultWallpaper : 'images/wallpaper.jpg',
		/**
		 * @property 墙纸路径
		 * */
		wallpaper :'images/wallpaper.jpg',
		
		/**
		 * @property 主题
		 */
		theme : 'blue',
		
		shortcutSize : 'small'
	};

	/**
	 * 初始化壁纸模块
	 */
	HteOS.Settings.init = function() {
		// 显示壁纸
		if(HteOS.User.wallpaper){
			this.wallpaper = HteOS.User.wallpaper;
		}
		var wallpaper = this.wallpaper;
		var dom = $("<div class=\"hte-wallpaper\">" +
						"<img class=\"hte-wallpaper-image\" src=\"" + this.wallpaper + "\" >" +
						"<img class=\"hte-wallpaper-image-back\">" +
					"</div>");
		HteOS.getContainer().append(dom);
		dom.find(".hte-wallpaper-image").load(function() {
			$(this).fadeIn();
		});
		// 设置背景图片
		HteOS.EventManager.on(["hte.metro.render"],function(){
			var app = HteOS.AppManager.getByKey("settings");
			if(HteOS.isMetro() && app){
				var tile = HteOS.metro.View.getTile(app.id);
				if (tile) {
					tile.getEl().find(".hte-tile-content").append('<img class="hte-tile-wallpaper-image" src="' + wallpaper + '">');
				}
			}
		});
		
		/*//监听墙纸更改事件，将墙纸保存到后台
		HteOS.EventManager.on("hte.wallpaper.change",function(src){
			HteOS.Settings.setWallpaper(src);
		});
		
		//监听主题更改事件，将主图保存到后台
		HteOS.EventManager.on("hte.theme.change",function(theme){
			HteOS.Settings.setTheme(theme);
		});*/
		
		//监听图标尺寸变化事件
		HteOS.EventManager.on("hte.desktop.shortcut.size.change",function(shortcutSize){
			HteOS.Settings.setShortcutSize(shortcutSize);
		});
	};

	/**
	 * 设置桌面背景
	 * @param {String} src
	 */
	HteOS.Settings.setWallpaper = function(wallpaper) {
		if(this.wallpaper == wallpaper){
			return ;
		}
		var back = $(".hte-wallpaper-image-back");
		back.stop();
		back.attr("src",wallpaper);
		back.off("error");
		back.off("load");
		back.load(function(){
			HteOS.Settings.swicthWallpaper(wallpaper);
		});
		back.error(function(){
			HteOS.Notification.show("","应用消息","加载壁纸失败，无法设置壁纸。");
		});
		HteOS.EventManager.trigger('hte.wallpaper.change',wallpaper);
	};
	
	/**
	 * 设置主题
	 * @param {String} src
	 */
	HteOS.Settings.setTheme = function(theme) {
		if(this.theme == theme){
			return;
		}
		HteOS.Settings.theme = theme;
		/*if (HteOS.UA.isIE8) {
			HteOS.Messager.alert("HteOS提示","您使用的浏览器不支持该主题，请升级您的浏览器");
		}*/
		$('<link rel="stylesheet">').appendTo($("body")).attr("href",HteOS.basePath + "css/" + theme + "/hteos.css").on('load',function(){
			$("[hte-theme]").remove();
			$(this).attr("hte-theme","");
		});
		HteOS.Settings.saveTheme(theme);
		HteOS.EventManager.trigger('hte.theme.change',this.theme);
	};
	
	/**
	 * 动态切换桌面背景
	 */
	HteOS.Settings.swicthWallpaper = function(src) {
		HteOS.Settings.wallpaper = src;
		var back = $(".hte-wallpaper-image-back");
		var front = $(".hte-wallpaper-image");
		front.stop();
		var wallpaperApp = HteOS.AppManager.getByKey("wallpaper");
		if (wallpaperApp) {
			wallpaperApp.getEl().find(".hte-tile-wallpaper-image").attr("src",
				HteOS.Settings.wallpaper);
		}
		back.fadeIn('slow');
		HteOS.Settings.saveWallpaper(HteOS.Settings.wallpaper);
		//front.show();
		front.fadeOut(1000, function() {
			front.attr('src', HteOS.Settings.wallpaper);
			front.show();
			back.hide();
		});
	};

	/**
	 * 保存桌面背景
	 * @param {String} src
	 */
	HteOS.Settings.saveWallpaper = function(src) {
		$.ajax({
			url: HteOS.API.WALLPAPER_SAVE,
			method: 'GET',
			data: {
				wallpaper: src
			},
			success: function(data) {
				if (!data.success) {
					HteOS.Notification.show("", "应用消息", data.message,
						true);
				}
			},
			error: function() {
				HteOS.Notification.show("", "应用消息", "保存背景图片失败，请稍候再试",
					true);
			}
		});
	};
	
	/**
	 * 保存主题
	 * @param {String} src
	 */
	HteOS.Settings.saveTheme = function(theme) {
		$.ajax({
			url: HteOS.API.THEME_SAVE,
			method: 'GET',
			data: {
				theme: theme
			},
			success: function(data) {
				if (!data.success) {
					HteOS.Notification.show("", "应用消息", data.message,
						true);
				}
			},
			error: function() {
				HteOS.Notification.show("", "应用消息", "保存主题失败，请稍候再试",
					true);
			}
		});
	};
	
	/**
	 * 设置图标的大小
	 */
	HteOS.Settings.setShortcutSize = function(shortcutSize){
		this.shortcutSize = shortcutSize;
		$.ajax({
			url : HteOS.API.SAVE_SHORTCUT_SIZE,
			method : HteOS.API_METHOD,
			data : {
				shortcutSize : shortcutSize
			}
		}).success(function(data){
			if (!data.success) {
				HteOS.Notification.show("", "应用消息", data.message,
						true);
			}
		});
	}
	
	/**
	 * 设置显示模式，桌面模式或者Metro模式
	 */
	HteOS.Settings.setMode = function(mode,fn){
		this.mode = mode;
		$.ajax({
			url : HteOS.API.SAVE_MODE,
			method : HteOS.API_METHOD,
			data : {
				mode : mode
			}
		}).success(function(data){
			if (!data.success) {
				HteOS.Notification.show("", "应用消息", data.message,
						true);
			}else{
				if(fn){
					fn();
				}
			}
		});
	}
	
}));